package cn.gailvlun.gll.chatkit;

import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import cn.gailvlun.gll.util.DensityUtil;
import cn.gailvlun.gll.util.LogUtil;
import cn.gailvlun.gll.widget.RotatedDrawable;
import io.reactivex.functions.Consumer;



public class ChatKitBindingAdapter {

    @BindingAdapter(value = {"imageResourceUrl", "isRound", "cornerRadius"}, requireAll = false)
    public static void loadImage(ImageView imageView, Object url, boolean isRound, float cornerRadius) {
        if (imageView != null) {
            if (url == null) {
                return;
            }

            RequestOptions requestOptions = RequestOptions
                    .placeholderOf(R.drawable.ic_placeholder_square)
                    .error(R.drawable.ic_placeholder_square);

            if (isRound) {
                if (cornerRadius == 0) {
                    requestOptions = requestOptions.circleCrop();
                } else {
                    requestOptions = requestOptions.transform(new RoundedCorners(DensityUtil.dp2px(imageView.getContext(), cornerRadius)));
                }
            }

            Glide.with(imageView.getContext())
                    .load(url)
                    .apply(requestOptions)
                    .into(imageView);
        }
    }

    @BindingAdapter("stickerPhoto")
    public static void loadStickerPhoto(final ImageView imageView, String localPath) {
        RequestOptions options = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.DATA)
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        final int scale = (int) imageView.getContext().getResources().getDisplayMetrics().density;
        Glide.with(imageView)
                .load(localPath)
                .apply(options)
                .into(new ImageViewTarget<Drawable>(imageView) {
                    @Override
                    protected void setResource(@Nullable Drawable resource) {
                        if (resource != null) {
//                            LogUtil.d("resource.getIntrinsicWidth: " + resource.getIntrinsicWidth());
//                            LogUtil.d("resource.getIntrinsicHeight: " + resource.getIntrinsicHeight());
                            imageView.getLayoutParams().width = resource.getIntrinsicWidth() * scale;
                            imageView.getLayoutParams().height = resource.getIntrinsicHeight() * scale;
                            imageView.setImageDrawable(resource);
                        }
                    }
                });
    }

    @BindingAdapter(value = {"chatImageUrl", "chatImageWidth", "chatImageHeight"})
    public static void loadImage(final ImageView imageView, Object url,
                                 final int chatImageWidth, final int chatImageHeight) {
        if (imageView != null) {
            imageView.requestLayout();
            if (chatImageWidth < 100 && chatImageHeight < 100) {
                
                imageView.getLayoutParams().width = chatImageWidth;
                imageView.getLayoutParams().height = chatImageHeight;
            } else {
                if (chatImageHeight / chatImageWidth > 3) {
                    imageView.getLayoutParams().height = ConvertUtils.dp2px(200);
                    imageView.getLayoutParams().width = ConvertUtils.dp2px(200 / 3);
                } else if (chatImageWidth / chatImageHeight > 3) {
                    imageView.getLayoutParams().height = ConvertUtils.dp2px(200 / 3);
                    imageView.getLayoutParams().width = ConvertUtils.dp2px(200);
                } else if (chatImageWidth / chatImageHeight >= 1) {
                    imageView.getLayoutParams().width = ConvertUtils.dp2px(200);
                    imageView.getLayoutParams().height = ConvertUtils.dp2px(200 * chatImageHeight / chatImageWidth);
                } else if (chatImageHeight / chatImageWidth >= 1) {
                    imageView.getLayoutParams().width = ConvertUtils.dp2px(200 * chatImageWidth / chatImageHeight);
                    imageView.getLayoutParams().height = ConvertUtils.dp2px(200);
                }
            }

            Glide.with(imageView.getContext())
                    .load(url)
                    .apply(RequestOptions.centerCropTransform())
                    .into(new ImageViewTarget<Drawable>(imageView) {
                        @Override
                        protected void setResource(@Nullable Drawable resource) {
                            if (resource != null) {
                                imageView.setImageDrawable(resource);
                            }
                        }
                    });
        }
    }

    
    @BindingAdapter(value = {"throttleClick", "throttleDuration"}, requireAll = false)
    public static void throttleClick(final View view, final View.OnClickListener onClickListener,
                                     Integer throttleDuration) {
        if (view == null) {
            return;
        }
        if (throttleDuration == null) {
            throttleDuration = 1000;
        }
        if (onClickListener != null) {
            RxView.clicks(view)
                    .throttleFirst(throttleDuration, TimeUnit.MILLISECONDS)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            onClickListener.onClick(view);
                        }
                    });
        }
    }

    @BindingAdapter(value = {"debounceClick"})
    public static void debounceClick(final View view, final View.OnClickListener onClickListener) {

    }

    @BindingAdapter("width")
    public static void setWidth(View view, float dp) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = DensityUtil.dp2px(view.getContext(), dp);
        view.setLayoutParams(lp);
    }

    @BindingAdapter("loadHeader")
    public static void loadHeader(ImageView iv, Object url) {
        if (iv != null) {
            RequestOptions requestOptions = RequestOptions
                    .placeholderOf(R.drawable.img_header_placeholder)
                    .error(R.drawable.img_header_placeholder)
                    .circleCrop();

            Glide.with(iv.getContext())
                    .load(url)
                    .apply(requestOptions)
                    .into(iv);
        }
    }

}
